package com.springboot.controller;

import com.springboot.model.ChatRecord;
import com.springboot.model.UserLoginForm;
import com.springboot.service.DoctorChattingListService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;

/**
 * 本控制台用于医生在聊天列表删除聊天
 */
@Controller
public class DoctorDeleteChatListController {
    @Autowired
    DoctorChattingListService doctorChattingListService;
    /**
     * 医生在聊天列表界面删除聊天
     */
    @CrossOrigin
    @RequestMapping(value = "/doctorDeleteChat")
    public @ResponseBody
    String doctorDeleteAssistant(@RequestParam("tel") String doctorTel,
                                 @RequestParam("otherTel") String otherTel) {
        System.out.println("医生删除聊天列表");
        try {
            UserLoginForm doctor=doctorChattingListService.findUserByTel(doctorTel);
            UserLoginForm other=doctorChattingListService.findUserByTel(otherTel);
            int doctorId=doctor.getUserId();
            int otherId=other.getUserId();
            ChatRecord chatRecord=new ChatRecord();
            chatRecord.setSenderId(doctorId);
            chatRecord.setReceiverId(otherId);
            ArrayList<ChatRecord> chatRecordArrayList=doctorChattingListService.findChatByRecord(chatRecord);
            for(int i=0;i<chatRecordArrayList.size();i++)
            {
                ChatRecord record=chatRecordArrayList.get(i);
                record.setDoctorFlag(1);
                doctorChattingListService.updateChatRecord(record);
            }
            ChatRecord record=new ChatRecord();
            record.setSenderId(otherId);
            record.setReceiverId(doctorId);
            ArrayList<ChatRecord> chatRecords=doctorChattingListService.findChatByRecord(record);
            for(int i=0;i<chatRecords.size();i++)
            {
                ChatRecord otherRecord=chatRecords.get(i);
                otherRecord.setDoctorFlag(1);
                doctorChattingListService.updateChatRecord(otherRecord);
            }
            return "True";
        } catch (Exception e) {
            return "False";
        }
    }
}
